import requests
from bs4 import BeautifulSoup
import csv
import time

csv_filename = 'new_weather_data.csv'
headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0'
}
for year in range(2019, 2024):
    for month in range(1, 13):
        this_year = str(year).strip()
        this_month = str(month).strip()
        url = 'http://www.tianqihoubao.com/lishi/nanchang/month/' + this_year + this_month + '.html'
        if month <= 9:
            url = 'http://www.tianqihoubao.com/lishi/nanchang/month/' + this_year + '0' + this_month + '.html'
        response = requests.get(url, headers=headers)

# 使用 BeautifulSoup 解析 HTML
        soup = BeautifulSoup(response.text, 'html.parser')

# 找到表格元素
        table = soup.find('table')
        with open(csv_filename, 'a', newline='', encoding='utf-8') as csv_file:
            writer = csv.writer(csv_file)
            # 找到表头
            headers = []
            for th in table.find_all('th'):
                headers.append(th.text.strip())
            writer.writerow(headers)
            for row in table.find_all('tr'):
                # 找到每一行中的单元格
                cells = row.find_all(['th', 'td'])

                # 打印单元格内容
                for cell in cells:
                    print(cell.text.strip(), end='\t')
                print()
                # 找到表格数据并写入 CSV 文件
            for row in table.find_all('tr'):
                data = []
                for td in row.find_all('td'):
                    data.append(td.text.strip())
                writer.writerow(data)
        response.close()
    time.sleep(5)
